﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Filemanager
{
    public partial class Process1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"];
                if (Request.QueryString["delete"] == "true")
                {
                    DeleteFile(urlFile(int.Parse(id)));
                    Request.QueryString["delete"] = null;
                }
                if ((Request.QueryString["rename"] == "true") && (Request.QueryString["name"] != null))
                {
                    Rename(urlFile(int.Parse(id)), Request.QueryString["name"]);
                    Request.QueryString["rename"] = null;
                }
            }
        }
        protected void Rename(string url, string newname)
        {
            FileInfo file = new FileInfo(url);
            string formatfile = file.Extension;
            int length = url.Length - url.Substring(url.LastIndexOf('\\')).Length;
            string des = url.Substring(0, length) + "\\" + newname + formatfile;
            File.Move(url, des);
            Response.Redirect(Request.UrlReferrer.PathAndQuery);
        }
        protected void DeleteFile(string url)
        {
            FileInfo file = new FileInfo(url);
            File.Delete(url);
            Response.Redirect(Request.UrlReferrer.PathAndQuery);
        }
        public string urlFile(int i)
        {
            string root = "";
            if (Request.QueryString["Root"] == null)
            {
                root = Server.MapPath(ConfigurationManager.AppSettings["PathParent"]);
            }
            else
            {
                root = Server.MapPath(Request.QueryString["Root"]);
            }
            Process pro = new Process();
            List<Folder> folderlist = new List<Folder>();
            pro.duyetThuMuc(root, folderlist);
            return Server.MapPath(folderlist[0].File[i]);

        }
    }
}